//
//  ChatCell.m
//  QQ界面
//
//  Created by apple on 16/11/18.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "ChatCell.h"
#import "ChatModel.h"
#import "ChatModelFrame.h"
@interface ChatCell()

//1.时间label
@property(nonatomic, weak)UILabel * lblTime;
//2.消息正文button
@property(nonatomic, weak)UIButton * btnText;
//3.头像image
@property(nonatomic, weak)UIImageView * imgViewIcon;

@end

@implementation ChatCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UILabel *lblTime = [[UILabel alloc]init];
        lblTime.font = [UIFont systemFontOfSize:12];
        lblTime.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:lblTime];
        self.lblTime = lblTime;
        UIImageView *imgViewIcon = [[UIImageView alloc]init];
        [self.contentView addSubview:imgViewIcon];
        self.imgViewIcon = imgViewIcon;
        UIButton *btnTxet = [[UIButton alloc]init];
        btnTxet.titleLabel.font = [UIFont systemFontOfSize:14];
        [btnTxet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnTxet.titleLabel.numberOfLines =0;
        btnTxet.contentEdgeInsets = UIEdgeInsetsMake(5, 15, 5, 13);
        [self.contentView addSubview:btnTxet];
        self.btnText =btnTxet;
    }
    return self;
}
-(void)setChatModelFrame:(ChatModelFrame *)chatModelFrame{
    _chatModelFrame = chatModelFrame;
    ChatModel *chatModel = chatModelFrame.chatModel;
    _lblTime.text =chatModel.time;
    _lblTime.frame = chatModelFrame.timeFrame;
    NSString * iconName = (chatModel.type==MessageTypeMe)?@"me":@"other";
    _imgViewIcon.image = [UIImage imageNamed:iconName];
    _imgViewIcon.frame = chatModelFrame.iconframe;
    //2.3 设置正文的数据和frame
    [_btnText setTitle:chatModel.text forState:UIControlStateNormal];
    _btnText.frame = chatModelFrame.textFrame;
    //2.3.2 设置正文内容的背景
    //判断消息发送者是谁
    NSString * imgNor,*imgHighLighted;
    if(chatModel.type == MessageTypeMe)
    {
        imgNor = @"chat_send_nor";
        imgHighLighted = @"chat_send_press_pic";
        //设置自己发的消息文字颜色
        [self.btnText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        imgNor = @"chat_recive_nor";
        imgHighLighted = @"chat_recive_press_nor";
        [self.btnText setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    //正文的size来修改气泡图片防止被变形
    UIImage * imageNor = [UIImage imageNamed:imgNor];
    UIImage * imageHighLighted = [UIImage imageNamed:imgHighLighted];
    //用平铺的方式去拉伸图片（4个角分开弄）
    imageNor = [imageNor stretchableImageWithLeftCapWidth:imageNor.size.width*0.5 topCapHeight:imageNor.size.height*0.54];
    
    imageHighLighted = [imageHighLighted stretchableImageWithLeftCapWidth:imageHighLighted.size.width*0.5 topCapHeight:imageHighLighted.size.height*0.5];
    //设置正文的背景气泡图片
    [self.btnText setBackgroundImage:imageNor forState:UIControlStateNormal];
    [self.btnText setBackgroundImage:imageHighLighted forState:UIControlStateHighlighted];
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
